﻿using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration;
using NationalD3.Domain.Entities;
using NationalD3.Domain.Events;
using NationalD3.Infrastructure.Data.Mappings;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace NationalD3.Infrastructure.Data.Context
{
    /// <summary>
    /// 事件存储数据库上下文，继承 DbContext
    /// 
    /// </summary>
    public class EventStoreSQLContext : DbContext
    {
        // 事件存储模型
        public DbSet<StoredEvent> StoredEvent { get; set; }

        protected override void OnModelCreating(ModelBuilder modelBuilder)
        {
            modelBuilder.ApplyConfiguration(new StoredEventMap());

            base.OnModelCreating(modelBuilder);
        }

        protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
        {
            //从配置文件读取连接字符串
            var config = new ConfigurationBuilder()
                .SetBasePath(Directory.GetCurrentDirectory())
                .AddJsonFile("appsettings.json")
                .Build();
            //判断是什么数据库
            if (config.GetSection("DbType").Value.Equals("MySql"))
            {
                var strConn = config.GetConnectionString("MySqlConnection");
                optionsBuilder.UseMySql(strConn, ServerVersion.AutoDetect(strConn));
            }
            else
            {
                var strConn = config.GetConnectionString("DefaultConnection");
                optionsBuilder.UseSqlServer(strConn); //使用sqlserver数据库
            }
        }
        //public DbSet<Student> Students { get; set; }
        //public DbSet<>  { get; set; }
    }
}
